package gemini.castor.ui.client.mvp;

import gemini.castor.ui.client.mvp.container.CastorPlaceContainer;
import gemini.castor.ui.client.mvp.container.CastorWidgetContainerPresenter;
import gemini.castor.ui.client.mvp.event.ContainerRequestEvent;
import gemini.castor.ui.client.mvp.event.ContainerRequestHandler;
import gemini.castor.ui.client.mvp.event.WidgetChangeEvent;
import gemini.castor.ui.client.page.content.account.accountinfo.AccountInfoPresenter;
import gemini.castor.ui.client.page.content.account.mydownline.MyDownlinePresenter;
import gemini.castor.ui.client.page.content.account.myorder.MyOrderPresenter;
import gemini.castor.ui.client.page.content.account.mysponsor.MySponsorPresenter;
import gemini.castor.ui.client.page.content.account.volumepoint.VolumePointPresenter;
import gemini.castor.ui.client.page.content.customer.careschedule.CareSchedulePresenter;
import gemini.castor.ui.client.page.content.customer.customerlist.CustomerPresenter;
import gemini.castor.ui.client.page.content.home.apply.ApplyPresenter;
import gemini.castor.ui.client.page.content.home.register.RegisterPresenter;
import gemini.castor.ui.client.page.content.home.search.SearchPresenter;
import gemini.castor.ui.client.page.content.information.aboutcompany.AboutCompanyPresenter;
import gemini.castor.ui.client.page.content.information.aboutproduct.AboutProductPresenter;
import gemini.castor.ui.client.page.content.information.bussinessmethod.BussinessMethodsPresenter;
import gemini.castor.ui.client.page.content.information.library.LibraryPresenter;
import gemini.castor.ui.client.page.content.information.maketingplan.MaketingPlanPresenter;
import gemini.castor.ui.client.page.content.information.stepbystep.StepByStepPresenter;
import gemini.castor.ui.client.page.content.information.successstories.SuccessStoriesPresenter;
import gemini.castor.ui.client.page.content.office.events.EventsPresenter;
import gemini.castor.ui.client.page.content.office.messegercenter.MessegerCenterPresenter;
import gemini.castor.ui.client.page.content.office.mystory.MyStoryPresenter;
import gemini.castor.ui.client.page.content.office.news.NewsPresenter;
import gemini.castor.ui.client.page.content.office.promotions.PromotionsPresenter;
import gemini.castor.ui.client.page.content.office.recognitions.RecognitionsPresenter;
import gemini.castor.ui.client.page.content.office.todolist.TodoListPresenter;
import gemini.castor.ui.client.page.content.store.catalog.CatalogPresenter;
import gemini.castor.ui.client.page.content.store.pricelist.PriceListPresenter;
import gemini.castor.ui.client.page.content.store.searchproduct.SearchProductPresenter;
import gemini.castor.ui.client.page.content.store.shoppingcart.ShoppingCartPresenter;
import gemini.castor.ui.client.utils.PlaceConstants.LayoutPosition;

import java.util.HashMap;

import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;
import ch.elca.gwt.mvp.client.place.PlaceChangedEvent;
import ch.elca.gwt.mvp.client.place.PlaceRequest;

import com.google.inject.Inject;

public class PlaceRequestManager {
	private final EventBus globalEventBus;
	private static HashMap<String, Place> placeMapper = new HashMap<String, Place>();

	@Inject
	public PlaceRequestManager(final EventBus globalEventBus) {
		this.globalEventBus = globalEventBus;
	}

	public void registerHandlers() {
		this.globalEventBus.addHandler(ContainerRequestEvent.getType(),
				new ContainerRequestHandler() {
					@Override
					public void onRequest(final ContainerRequestEvent event) {
						CastorPlaceContainer<?> newPlace = (CastorPlaceContainer<?>) getPlace(event.getRequestPlaceId());
						gotoCastorContainerRequestedPlace(event, newPlace, event.getObject());
					}
				});
	}
	
	private CastorWidgetContainerPresenter<?> presenter;
	
	private void gotoCastorContainerRequestedPlace(final ContainerRequestEvent event,
			final CastorPlaceContainer<?> requestedPlace, Object ob) {
		if (presenter != null && presenter.isBound()) {
			presenter.unbind();
		}

		presenter = (CastorWidgetContainerPresenter<?>)requestedPlace.getPresenter();

		if(ob != null){
			presenter.setInputObject(ob);
		}
		
		if (!presenter.isBound()) {
			presenter.bind();
		}
		
		globalEventBus.fireEvent(new WidgetChangeEvent(presenter, event.getPosition()));
		
		if (event.getPosition() == null || event.getPosition().equals(LayoutPosition.CONTENT)) {
			globalEventBus.fireEvent(new PlaceChangedEvent(new PlaceRequest(
					presenter.getPlace())));
		}
	}
	
	private static void intPlaceStore() {
		putPlace(ApplyPresenter.PLACE);
		putPlace(RegisterPresenter.PLACE);
		putPlace(SearchPresenter.PLACE);
		putPlace(CatalogPresenter.PLACE);
		putPlace(ShoppingCartPresenter.PLACE);
		putPlace(SearchProductPresenter.PLACE);
		putPlace(PriceListPresenter.PLACE);
		putPlace(AccountInfoPresenter.PLACE);
		putPlace(VolumePointPresenter.PLACE);
		putPlace(MyOrderPresenter.PLACE);
		putPlace(MySponsorPresenter.PLACE);
		putPlace(MyDownlinePresenter.PLACE);
		putPlace(MyStoryPresenter.PLACE);
		putPlace(EventsPresenter.PLACE);
		putPlace(MessegerCenterPresenter.PLACE);
		putPlace(NewsPresenter.PLACE);
		putPlace(PromotionsPresenter.PLACE);
		putPlace(TodoListPresenter.PLACE);
		putPlace(RecognitionsPresenter.PLACE);
		putPlace(CareSchedulePresenter.PLACE);
		putPlace(CustomerPresenter.PLACE);
		putPlace(SuccessStoriesPresenter.PLACE);
		putPlace(StepByStepPresenter.PLACE);
		putPlace(MaketingPlanPresenter.PLACE);
		putPlace(LibraryPresenter.PLACE);
		putPlace(BussinessMethodsPresenter.PLACE);
		putPlace(AboutCompanyPresenter.PLACE);
		putPlace(AboutProductPresenter.PLACE);
    }

    private static void putPlace(final Place place) {
        placeMapper.put(place.getId(), place);
    }

    public static Place getPlace(final String key) {
        if (placeMapper.isEmpty()) {
            intPlaceStore();
        }
        return placeMapper.get(key);
    }
}
